# Contributor: Carbon Jiao <http://archlive-pkg.googlecode.com>

pkgname=firefox-pgo-archlive
pkgver=3.5.4
pkgrel=1
_ff_ver=3.5
_ff_identifier={ec8030f7-c20a-464f-9b0e-13a3a9e97384}
pkgdesc="Mozilla Firefox中文版 - tracemonkey-enabled, XULRunner independent, optimized with PGO - real standalone 无插件版"
arch=('i686' 'x86_64')
license=('MPL' 'GPL' 'LGPL')
	if [ `uname -m` = 'i686' ]; then 
makedepends=('zip' 'pkgconfig' 'diffutils' 'python' 'xorg-server' 'imagemagick' 'alsa-lib' 'file' 'autoconf-compat')
	else
makedepends=('zip' 'pkgconfig' 'diffutils' 'python' 'xorg-server' 'imagemagick' 'alsa-lib' 'file' 'autoconf-compat' 'gcc-snapshot')
	fi
depends=('gtk2>=2.16.0' 'gcc-libs>=4.4' 'libidl2>=0.8.13' 'mozilla-common' 'nss>=3.12.3' 'libxt' 'startup-notification>=0.9' 'desktop-file-utils' 'libjpeg' 'libpng' 'cairo' 'hunspell')
provides=('firefox'="$pkgver")
conflicts=('firefox' 'firefox-archlive')
options=('!makeflags' 'ccache')
install=firefox.install
url="http://www.mozilla.org/projects/firefox"
source=(
  ftp://ftp.mozilla.org/pub/mozilla.org/firefox/releases/"$pkgver"/source/firefox-"$pkgver"-source.tar.bz2
  ftp://ftp.mozilla.org/pub/mozilla.org/firefox/releases/"$pkgver"/linux-i686/zh-CN/firefox-"$pkgver".tar.bz2
  mozconfig	mozconfig.x86_64 100-system-hunspell-corrections.patch ldflags.patch ldflags2.patch
  firefox.desktop	firefox-safe.desktop  prefs.js  distribution.ini  firefox-quick
  http://fpdownload.macromedia.com/get/flashplayer/current/install_flash_player_10_linux.tar.gz
)
md5sums=()

build() {
  cd ${startdir}/src/mozilla-1.9.1
	if [ "${CARCH}" = 'i686' ]; then
  cp ${startdir}/src/mozconfig .mozconfig
	else
  cp ${startdir}/src/mozconfig.x86_64 .mozconfig
	fi

  #fix build with system hunspell - gentoo
  patch -Np0 -i ${srcdir}/100-system-hunspell-corrections.patch || return 1
  patch -Np1 -i ${srcdir}/ldflags.patch || return 1

  #PGO patch - use when compiling stops with control sum mismatch error, thanks to methuselah
  #patch -Np1 -i ${srcdir}/ldflags2.patch || return 1

#  LD_PRELOAD="" /usr/bin/Xvfb -nolisten tcp -extension GLX :99 &
#  XPID=$!
#  export DISPLAY=:99

#  unset LD_PRELOAD
#  unset CFLAGS
#  unset CXXFLAGS
  export CFLAGS="-march=native"
  export CXXFLAGS="-march=native"
  export LDFLAGS="-Wl,-rpath,/usr/lib/firefox-${_ff_ver} -Wl,--hash-style=gnu,--sort-common"

#for pgo build
  make -f client.mk profiledbuild MOZ_MAKE_FLAGS="${MAKEFLAGS}" || return 1
#
#for non-pgo build
#  make -f client.mk MOZ_MAKE_FLAGS="${MAKEFLAGS}" || return 1
#
  kill $XPID
  make -j2 DESTDIR=${pkgdir} -C ff-pgo install || return 1

  install -m755 -d ${pkgdir}/usr/share/applications
  install -m755 -d ${pkgdir}/usr/share/pixmaps
  convert "$srcdir"/mozilla-1.9.1/other-licenses/branding/firefox/mozicon50.xpm ${pkgdir}/usr/share/pixmaps/firefox.png || return 1
  install -m644 ${srcdir}/firefox.desktop ${pkgdir}/usr/share/applications/ || return 1
  install -m644 ${srcdir}/firefox-safe.desktop ${pkgdir}/usr/share/applications/ || return 1
#Remove devel stuff
  rm -rf "$pkgdir"/usr/include/
  rm -rf "$pkgdir"/usr/lib/firefox-devel-${pkgver}/
  rm -rf "$pkgdir"/usr/lib/firefox-devel-${_ff_ver}/
  rm -rf "$pkgdir"/usr/share/idl/

  #卸载默认插件
  rm -rf ${pkgdir}/usr/lib/firefox-${_ff_ver}/extensions

  #安装中文语言包
  install -m755 $srcdir/firefox/chrome/{zh-CN.jar,zh-CN.manifest} $pkgdir/usr/lib/firefox-${_ff_ver}/chrome/
  #将中文设置为默认语言
  sed -i "s/\"en-US\"/\"zh-CN\"/g" $pkgdir/usr/lib/firefox-${_ff_ver}/defaults/pref/firefox-l10n.js

  #安装archlive预配置文件及快速启动文件
  install -d ${pkgdir}/usr/lib/firefox-${pkgver}/distribution
  install -m644 ${srcdir}/distribution.ini ${pkgdir}/usr/lib/firefox-${_ff_ver}/distribution/
  sed -i "s|version=.*|version=$pkgver|g" ${pkgdir}/usr/lib/firefox-${_ff_ver}/distribution/distribution.ini

  [ -e $pkgdir/usr/lib/firefox-${_ff_ver}/firefox ] && (cp -f $pkgdir/usr/lib/firefox-${_ff_ver}/{firefox,firefox-old})
  install -m755 $srcdir/firefox $pkgdir/usr/lib/firefox-${_ff_ver}/firefox
  install -m777 $srcdir/prefs.js  $pkgdir/usr/lib/firefox-${_ff_ver}/

  #安装Flash 插件
  install -m755 $srcdir/libflashplayer.so $pkgdir/usr/lib/firefox-3.5/plugins/
}